
import { _decorator, Node, CircleCollider2D, Vec2, AudioClip, AudioSource, Vec3, animation, misc, RaycastResult2D } from 'cc';
import { Enemy } from './animy/Enemy';
import { ColliderTag, eventTarget, ObjectAtks } from './Globle';
import { MoveObject } from './MoveObject';
const { ccclass, property } = _decorator;

@ccclass('Thunder')
export class Thunder extends MoveObject {
    @property(AudioClip) thunder_fly_audio: AudioClip;
    @property(AudioClip) thunder_bomb_audio: AudioClip;
    public ATK = ObjectAtks.thunder;
    private collider: CircleCollider2D;
    private audioSource: AudioSource;
    private sprite: Node;
    private animator: animation.AnimationController;
    private speed: number = 2;
    private direction:Vec2;
    private isBomb:boolean = false;
    private start_position:Vec3;
    //飞行距离
    private fly_distance:number = 100;
    onLoad() {
        this.audioSource = this.getComponent(AudioSource);
        this.sprite = this.node.children[0];
        this.collider = this.getComponent(CircleCollider2D);
        this.animator = this.getComponent(animation.AnimationController);
    }

    onRayCast(rays:Readonly<RaycastResult2D[]>[]) {
        for(let i of rays){
            if(!i||!i.length) continue;
            for(let ray of i){
                switch (ray.collider.tag) {
                    case ColliderTag.attackedWall:
                        eventTarget.emit('destoryAttackedWall',ray.collider.node.uuid);
                        this.bomb(false);
                        break;
                    case ColliderTag.enemy://敌人
                        let obj = ray.collider.node.getComponent(Enemy);
                        if (obj) obj.onContact(this.collider);
                        this.bomb(false);
                        break;
                    case ColliderTag.enemy_people:
                        obj = ray.collider.node.getComponent(Enemy);
                        if (obj) obj.onContact(this.collider);
                        break;
                }
            }
            
        }
        
    }

    /**
     * setDirection
     */
    public setDirection(direction: Vec2, speed: number, tag: ColliderTag,fly_distance?:number) {
        this.direction = direction;
        this.speed = speed;
        this.start_position = this.node.position.clone();
        if(fly_distance) this.fly_distance = fly_distance;
        this.audioSource.clip = this.thunder_fly_audio;
        this.audioSource.play();
        this.collider.tag = tag;
        let radian = direction.signAngle(new Vec2(0,1));
        let deg = misc.radiansToDegrees(radian);
        this.node.eulerAngles = new Vec3(0,0,-deg)
    }

    /**
     * bomb
     */
    public bomb(liner = true) {
        this.isBomb = true;
        this.animator.setValue('isBomb',true);
        if (liner) {
            this.audioSource.stop();
            this.audioSource.clip = this.thunder_bomb_audio;
            this.audioSource.play();
        }
    }

    public onDied() {
        this.sprite.active = false;
        eventTarget.emit('thunderDestory', this.node);
        this.scheduleOnce(() => {this.node.destroy();}, 0.5)
    }

    update (deltaTime: number) {
        if(this.isBomb) return;
        super.update(deltaTime);
        this.node.position = new Vec3(this.node.position.x+this.direction.x*this.speed,this.node.position.y+this.direction.y*this.speed)
        let distance = this.node.position.clone().subtract(this.start_position).length();
        if(distance>this.fly_distance) this.bomb();
    }
}
